package com.qianyu.project.mapper.provider.sy;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qianyu.project.entity.IncomeContract;
import com.qianyu.project.vo.IncomeContractVo;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;

import java.util.Map;

public class IncomeContractProvider {
    /**
     * 获取收入合同列表
     * @param iPage
     * @param params
     * @return
     */
    public String selectPageData(IPage iPage, Map<String, String> params){
        return new SQL(){
            {
                SELECT("i.*, p.project_name, t1.company_name party_a, t2.company_name party_b");
                FROM("income_contract i, project_info p, tender_company t1, tender_company t2");
                WHERE("i.pid = p.id and i.cust_id = t1.id and i.second_party_id = t2.id");
                if(StringUtils.hasLength(params.get("id"))){
                    WHERE(" i.id = #{params.id}");
                }
                if(StringUtils.hasLength(params.get("contractName"))){
                    WHERE(" i.contract_name like '%${params.contractName}%'");
                }
                if(StringUtils.hasLength(params.get("pid"))){
                    WHERE(" i.pid = #{params.pid}");
                }
                if (StringUtils.hasLength(params.get("approveStatus"))){
                    WHERE(" i.approve_status = #{params.approveStatus}");
                }
                if (StringUtils.hasLength(params.get("contractType"))){
                    WHERE(" i.contract_type = #{params.contractType}");
                }
                if (StringUtils.hasLength(params.get("contractSignatory"))){
                    WHERE(" i.contract_signatory like '%${params.contractSignatory}%'");
                }
                if(StringUtils.hasLength(params.get("createDate1")) && StringUtils.hasLength(params.get("createDate2"))){
                    WHERE(" i.create_date >= '${params.createDate1}' and i.create_date <= '${params.createDate2}'");
                }
                if(StringUtils.hasLength(params.get("custId"))){
                    WHERE(" i.cust_id = #{params.custId}");
                }
            }
        }.toString();
    }

    /**
     * 用id获取收入合同
     * @param id
     * @return
     */
    public String getOne(String id) {
        return new SQL() {
            {
                SELECT("i.*, p.project_name, t1.company_name party_a, t2.company_name party_b");
                FROM("income_contract i, project_info p, tender_company t1, tender_company t2");
                WHERE("i.pid = p.id and i.cust_id = t1.id and i.second_party_id = t2.id and i.id = #{id}");
            }
        }.toString();
    }

    /**
     * 用pid获取收入合同
     *
     * @param id
     * @return
     */
    public String selectByPid(String id) {
        return new SQL() {
            {
                SELECT("i.*, p.project_name, t1.company_name party_a, t2.company_name party_b, t1.id aid, t2.id bid");
                FROM("income_contract i, project_info p, tender_company t1, tender_company t2");
                WHERE("i.pid = p.id and i.cust_id = t1.id and i.second_party_id = t2.id and i.pid = #{id}");
            }
        }.toString();
    }

    /**
     * 获取收入合同明细
     * @param page
     * @param params
     * @return
     */
    public String selectInfo(IPage page, Map<String, String> params) {
        return new SQL() {
            {
                SELECT("i.*, p.project_name, t1.company_name party_a, t2.company_name party_b, w.*, w.id no");
                FROM("income_contract i, project_info p, tender_company t1, tender_company t2, income_work_amount w");
                WHERE("i.pid = p.id and i.cust_id = t1.id and i.second_party_id = t2.id and i.id = w.pid");
                if(StringUtils.hasLength(params.get("id"))){
                    WHERE(" i.id = #{params.id}");
                }
                if(StringUtils.hasLength(params.get("contractName"))){
                    WHERE(" i.contract_name like '%${params.contractName}%'");
                }
                if(StringUtils.hasLength(params.get("pid"))){
                    WHERE(" i.pid = #{params.pid}");
                }
                if (StringUtils.hasLength(params.get("approveStatus"))){
                    WHERE(" i.approve_status = #{params.approveStatus}");
                }
                if (StringUtils.hasLength(params.get("contractType"))){
                    WHERE(" i.contract_type = #{params.contractType}");
                }
                if (StringUtils.hasLength(params.get("contractSignatory"))){
                    WHERE(" i.contract_signatory like '%${params.contractSignatory}%'");
                }
                if(StringUtils.hasLength(params.get("createDate1")) && StringUtils.hasLength(params.get("createDate2"))){
                    WHERE(" i.create_date >= '${params.createDate1}' and i.create_date <= '${params.createDate2}'");
                }
                if(StringUtils.hasLength(params.get("custId"))){
                    WHERE(" i.cust_id = #{params.custId}");
                }
                if(StringUtils.hasLength(params.get("name"))){
                    WHERE("w.name like '%${params.name}%'");
                }
            }
        }.toString();
    }

    /**
     * 获取收入合同报表
     *
     * @param page
     * @param params
     * @return
     */
    public String selectReport(IPage page, Map<String, String> params) {
        return new SQL() {
            {
                SELECT("i.contract_amount, c.change_amount, g.agree_amount, i.advance_received, " +
                        "g.agree_amount-i.advance_received  settle_outstanding_amount, " +
                        "i.contract_amount+c.change_amount-i.advance_received accounts_receivable,  " +
                        "p.project_name, t.company_name party_a");
                FROM("income_contract i, project_info p, tender_company t, contract_change c, progress_payment g");
                WHERE("i.pid = p.id and i.cust_id = t.id and i.id = c.ctr_id and i.id = g.ctr_id");
                if (StringUtils.hasLength(params.get("pid"))) {
                    WHERE(" i.pid = #{params.pid}");
                }
                if (StringUtils.hasLength(params.get("createDate1")) && StringUtils.hasLength(params.get("createDate2"))) {
                    WHERE(" i.create_date >= '${params.createDate1}' and i.create_date <= '${params.createDate2}'");
                }
                if (StringUtils.hasLength(params.get("custId"))) {
                    WHERE(" i.cust_id = #{params.custId}");
                }
            }
        }.toString();
    }


}
